package com.loong.news.controller.backend;

import com.loong.common.mvc.BaseBackendController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.news.common.backend.param.BatchOperationNewsParam;
import com.loong.news.common.backend.param.NewsListQueryParam;
import com.loong.news.common.backend.param.NewsParam;
import com.loong.news.handler.NewsHandler;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 处理新闻请求的controller
 * @Author: 刘群
 * @Date: 2020/10/14 17:39
 */
@RestController
@RequestMapping("manage/news/news")
public class NewsController extends BaseBackendController {
    @Resource
    private NewsHandler newsHandler;

    /**
     * 分页查询普通新闻
     * @param param 查询参数
     * @return 响应的结果
     */
    @PostMapping("newsList/pageQuery")
    public Result newsListPageQuery(@Validated({CommonValidate.class}) NewsListQueryParam param){
        return handlerExecutor.execute(() -> newsHandler.newsListPageQuery(param));
    }

    /**
     * 分页查询历史新闻
     * @param param 查询参数
     * @return 响应的结果
     */
    @PostMapping("history/pageQuery")
    public Result historyNewsPageQuery(NewsListQueryParam param){
        return handlerExecutor.execute(() -> newsHandler.historyNewsPageQuery(param));
    }

    /**
     * 预览新闻
     * @param param 新闻id
     * @return 响应的结果
     */
    @PostMapping("preview")
    public Result previewNews(@Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> newsHandler.findNewsById(param.getId()));
    }

    /**
     * 编辑新闻之后，批准通过新闻
     * @param newsParam 新闻参数
     * @return 响应的结果
     */
    @PostMapping("edit/approve")
    public Result editAndApproveNews(@Validated({CommonUpdate.class})NewsParam newsParam){
        return handlerExecutor.execute(() -> newsHandler.editAndApproveNews(newsParam));
    }

    /**
     * 编辑新闻之后，驳回新闻
     * @param newsParam 新闻参数
     * @return 响应的结果
     */
    @PostMapping("edit/reject")
    public Result editAndRejectNews(@Validated({CommonUpdate.class})NewsParam newsParam){
        return handlerExecutor.execute(() -> newsHandler.editAndRejectNews(newsParam));
    }

    /**
     * 编辑新闻之后，发布新闻
     * @param newsParam 新闻参数
     * @return 响应的结果
     */
    @PostMapping("edit/release")
    public Result editAndReleaseNews(@Validated({CommonUpdate.class})NewsParam newsParam){
        return handlerExecutor.execute(() -> newsHandler.editAndReleaseNews(newsParam));
    }

    /**
     * 编辑新闻之后，下架新闻
     * @param newsParam 新闻参数
     * @return 响应的结果
     */
    @PostMapping("edit/remove")
    public Result editAndRemoveNews(@Validated({CommonUpdate.class})NewsParam newsParam){
        return handlerExecutor.execute(() -> newsHandler.editAndRemoveNews(newsParam));
    }

    /**
     * 编辑新闻之后，直接保存新闻
     * @param newsParam 新闻参数
     * @return 响应的结果
     */
    @PostMapping("edit/save")
    public Result editAndSaveNews(@Validated({CommonUpdate.class})NewsParam newsParam){
        return handlerExecutor.execute(() -> newsHandler.editAndSaveNews(newsParam));
    }

    /**
     * 批量通过新闻
     * @param param 批量操作新闻的参数
     * @return 响应的结果
     */
    @PostMapping("batchApprove")
    public Result batchApprovalNews(@Validated({CommonValidate.class}) BatchOperationNewsParam param){
        return handlerExecutor.execute(() -> newsHandler.batchApprovalNews(param));
    }

    /**
     * 批量驳回新闻
     * @param param 批量操作新闻的参数
     * @return 响应的结果
     */
    @PostMapping("batchReject")
    public Result batchRejectNews(@Validated({CommonValidate.class}) BatchOperationNewsParam param){
        return handlerExecutor.execute(() -> newsHandler.batchRejectNews(param));
    }

    /**
     * 批量置顶新闻
     * @param param 批量操作新闻的参数
     * @return 响应的结果
     */
    @PostMapping("batchTop")
    public Result batchTopNews(@Validated({CommonValidate.class}) BatchOperationNewsParam param){
        return handlerExecutor.execute(() -> newsHandler.batchTopNews(param));
    }

    /**
     * 批量撤销置顶新闻
     * @param param 批量操作新闻的参数
     * @return 响应的结果
     */
    @PostMapping("batchRevoke")
    public Result batchRevokeNews(@Validated({CommonValidate.class}) BatchOperationNewsParam param){
        return handlerExecutor.execute(() -> newsHandler.batchRevokeNews(param));
    }

    /**
     * 批量下架新闻
     * @param param 批量操作新闻的参数
     * @return 响应的结果
     */
    @PostMapping("batchRemove")
    public Result batchRemoveNews(@Validated({CommonValidate.class}) BatchOperationNewsParam param){
        return handlerExecutor.execute(() -> newsHandler.batchRemoveNews(param));
    }

    /**
     * 批量删除新闻
     * @param idList 新闻id集合
     * @return 响应的结果
     */
    @PostMapping("batchDelete")
    public Result batchDeleteNews(@RequestParam("idList")List<Long> idList){
        if (CollectionUtils.isEmpty(idList)) return null;
        return handlerExecutor.execute(() -> newsHandler.batchDeleteNews(idList));
    }

    /**
     * 批量重新通过新闻
     * @param param 批量操作新闻的参数
     * @return 响应的结果
     */
    @PostMapping("batchReApprove")
    public Result batchReApproveNews(@Validated({CommonValidate.class}) BatchOperationNewsParam param){
        return handlerExecutor.execute(() -> newsHandler.batchReApproveNews(param));
    }

    /**
     * 批量发布新闻
     * @param param 批量操作新闻的参数
     * @return 响应的结果
     */
    @PostMapping("batchRelease")
    public Result batchReleaseNews(@Validated({CommonValidate.class}) BatchOperationNewsParam param){
        return handlerExecutor.execute(() -> newsHandler.batchReleaseNews(param));
    }

    /**
     * 新增并保存新闻
     * @param param 新闻参数
     * @return 响应的结果
     */
    @PostMapping("create/save")
    public Result createAndSaveNews(@Validated({CommonInsert.class})NewsParam param){
        param.setId(null);
        return handlerExecutor.execute(() -> newsHandler.createAndSaveNews(param));
    }

    /**
     * 新增并发布新闻
     * @param param 新闻参数
     * @return 响应的结果
     */
    @PostMapping("create/release")
    public Result createAndReleaseNews(@Validated({CommonInsert.class})NewsParam param){
        param.setId(null);
        return handlerExecutor.execute(() -> newsHandler.createAndReleaseNews(param));
    }

    /**
     * 查询新闻来源
     * @return 响应的结果
     */
    @PostMapping("queryPress")
    public Result findPress(){
        return handlerExecutor.execute(() -> newsHandler.findPress());
    }

    /**
     * 拉取新闻
     * @return 响应的结果
     */
    @PostMapping("pull")
    public Result pullNews(){
        return handlerExecutor.execute(() -> newsHandler.pullNews());
    }

}
